/**
 * @file    drv_rtc.c
 * @brief   RTC时钟驱动
 * @author  name
 * @version 1.0.1
 * @date    2023-04-14
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-14 <td>1.0.1     <td>name     <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "drv_rtc.h"
#include "mcu_type.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief  初始化RTC
 * @param  void
 * @return void
*/
uint8_t drv_rtc_init(rtc_data_type rtc_init_time)
{
    S_RTC_TIME_DATA_T sReadTime;

	SYS_UnlockReg();
	
	//CLK->PWRCTL |= CLK_PWRCTL_LXTEN_Msk;
	//* Set X32_OUT(PF.4) and X32_IN(PF.5) to input mode */
   // PF->MODE &= ~(GPIO_MODE_MODE4_Msk | GPIO_MODE_MODE5_Msk);

	//CLK_WaitClockReady(CLK_STATUS_LXTSTB_Msk);

	CLK->APBCLK0 |= CLK_APBCLK0_RTCCKEN_Msk; // RTC Clock Enable
	
	CLK->CLKSEL3 |= CLK_CLKSEL3_RTCSEL_Msk;
	
	SYS_LockReg();
	
	/* Set Tick Period */
    RTC_SetTickPeriod(RTC_TICK_1_SEC);

	RTC_Open(0);
	
    RTC_GetDateAndTime(&sReadTime);
	
    if(sReadTime.u32Year < rtc_init_time.year)
    {
        drv_rtc_set_date_time(rtc_init_time);
    }
	
	return TRUE;
}

/**
 * @brief 读取日期和时间
 * @return rtc_data_type :读取到的值
 */
uint8_t drv_rtc_read_date_time(rtc_data_type *rtc_data)
{
	S_RTC_TIME_DATA_T reg_time = {0};

	RTC_GetDateAndTime(&reg_time);

	rtc_data->year = reg_time.u32Year;
	rtc_data->month = reg_time.u32Month;
	rtc_data->day = reg_time.u32Day;
	rtc_data->hour = reg_time.u32Hour;
	rtc_data->min = reg_time.u32Minute;
	rtc_data->sec = reg_time.u32Second;
	rtc_data->week = reg_time.u32DayOfWeek;

	return TRUE;
}

/**
 * @brief  设置日期和时间
 * @param  set_val : 设置值
 */
uint8_t drv_rtc_set_date_time(rtc_data_type set_val)
{
	S_RTC_TIME_DATA_T set_time = {0};
	
	set_time.u32Year       = set_val.year;
   	set_time.u32Month      = set_val.month;
    set_time.u32Day        = set_val.day;
    set_time.u32Hour       = set_val.hour;
    set_time.u32Minute     = set_val.min;
    set_time.u32Second     = set_val.sec;
    set_time.u32DayOfWeek  = set_val.week;
    set_time.u32TimeScale  = RTC_CLOCK_24;

	RTC_SetDateAndTime(&set_time);

	return TRUE;
}
/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
